package com.ticket.controller.api;

import com.ticket.entity.User;
import com.ticket.service.IUserService;
import com.ticket.utils.Result;
import com.ticket.utils.UserContext;
import com.ticket.vo.PasswordUpdateVO;
import com.ticket.vo.UserProfileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping("/info")
    public Result<UserProfileVO> getUserInfo() {
        Long userId = UserContext.getCurrentUserId();
        User user = userService.getById(userId);
        
        UserProfileVO profile = new UserProfileVO();
        profile.setUsername(user.getUsername());
        profile.setEmail(user.getEmail());
        profile.setPhone(user.getPhone());
        profile.setAvatarUrl(user.getAvatarUrl());
        
        return Result.success(profile);
    }

    @PutMapping("/profile")
    public Result<Void> updateProfile(@RequestBody UserProfileVO profile) {
        Long userId = UserContext.getCurrentUserId();
        userService.updateProfile(userId, profile);
        return Result.success();
    }

    @PutMapping("/avatar")
    public Result<Void> updateAvatar(@RequestBody UserProfileVO profile) {
        Long userId = UserContext.getCurrentUserId();
        userService.updateAvatar(userId, profile.getAvatarUrl());
        return Result.success();
    }

    @PutMapping("/password")
    public Result<Void> updatePassword(@RequestBody PasswordUpdateVO passwordVO) {
        Long userId = UserContext.getCurrentUserId();
        userService.updatePassword(userId, passwordVO);
        return Result.success();
    }
} 